package com.lyn.image_tools;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;

import com.lyn.image_tools.databinding.ActivityMainBinding;
import com.lyn.module_custom_camera.YuvUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {


    private ActivityMainBinding binding;
    private NV21ToBitmap nv21ToBitmap;
    private ImageUtil imageUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        nv21ToBitmap = new NV21ToBitmap(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        imageUtil = new ImageUtil();
        SurfaceHolder holder = binding.LynSurface.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
               Camera camera=Camera.open(1);
                Camera.Parameters parameters = camera.getParameters();
                parameters.setPreviewSize(640,480);
                camera.setParameters(parameters);
                camera.addCallbackBuffer(new byte[640*480*3/2]);
                camera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() {
                    @Override
                    public void onPreviewFrame(byte[] data, Camera camera) {
                        byte[] rotateNV21 = imageUtil.rotateNV21(data, 640, 480, ImageUtil.Rotate.ROTATE_270);
                        byte[] cutNV21 = imageUtil.centerCutNV21(rotateNV21, 480, 640, 400, 480);
                        imageUtil.mirrorNV21(cutNV21,400,480);
                        int[] rgb = imageUtil.nv21ToRgb(cutNV21, 400, 480, true);
                        imageUtil.showRgbToSurface(rgb,400,480);
                        camera.addCallbackBuffer(data);
                    }
                });
                try {
                    camera.setPreviewDisplay(holder);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                camera.startPreview();
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

            }
        });

        SurfaceHolder holder1 = binding.LynSurface2.getHolder();
        holder1.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder1.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                imageUtil.setSurface(holder.getSurface());
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

            }
        });

    }
}